package com.lj.api.v1.c;

import com.alibaba.fastjson.JSONObject;
import com.lj.api.common.CodeMsg;
import com.lj.api.common.Result;
import com.lj.api.common.WebUtil;
import com.lj.api.tools.AccessoryTools;
import com.lj.mcore.entity.*;
import com.lj.mcore.service.IPromoService;
import com.lj.mcore.tools.PromoTools;
import com.lj.mcore.utils.CommUtils;
import com.lj.mcore.utils.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/c")
public class PromoController {

    @Autowired
    IPromoService promoService;

    @RequestMapping("/promo_list")
    public String promo_list(String currentPage,String pageSize,String gid,String gcid,String sid){

        if (WebUtil.checkEmpty(currentPage,pageSize,sid)){
            return Result.fail(CodeMsg.PARAMETER_ISNULL);
        }


        int page = CommUtils.null2Int(currentPage)==0?1:CommUtils.null2Int(currentPage);
        Map query = new HashMap();
        query.put("scope",0);
        query.put("sid",sid);
        if (CommUtils.null2String(gid).equals("")){
            query.put("gid",gid);
        }

        if (!CommUtils.null2String(gcid).equals("")){
            query.put("gcid",gcid);
        }

        int pagesize = 200;
        pagesize=CommUtils.null2Int(pagesize)>200?pagesize:CommUtils.null2Int(pagesize);
        pagesize=CommUtils.null2Int(pagesize)<1?1:CommUtils.null2Int(pagesize);
        PageBean pagebean = promoService.getList(pagesize,page,query);
        List<Promo> list = pagebean.getList();

        List list1 = new ArrayList();
        for(Promo promo:list){
            Map map = new HashMap();
            map.put("addTime",CommUtils.formatTime("yyyy-MM-dd HH:mm:ss",promo.getAddTime()));
            map.put("pid",promo.getId());
            map.put("status",promo.getStatus());
            map.put("type",promo.getType());
            map.put("ruleText", PromoTools.transformToStr(promo.getType(),promo.getRules()));
            map.put("quota",promo.getQuota());
            map.put("startTime",CommUtils.formatTime("yyyy-MM-dd HH:mm:ss",promo.getStartTime()));
            map.put("endTime",CommUtils.formatTime("yyyy-MM-dd HH:mm:ss",promo.getEndTime()));

            Goods goods = promo.getGoods();
            if (goods!=null){
                map.put("gid",goods.getId());
                map.put("goodsName",goods.getGoods_name());
                Accessory accessory = goods.getGoods_main_photo();
                if (accessory!=null){
                    map.put("img", AccessoryTools.getProcessURL_QN(accessory,3));
                }else{
                    map.put("img","");
                }
            }

            GoodsClass goodsClass = promo.getGoodsClass();
            if (goods!=null){
                map.put("gcPath","");
            }else{
                map.put("gcPath","");
            }

            list1.add(map);
        }

        Map info = new HashMap();
        info.put("objs", JSONObject.toJSON(list1));
        info.put("totalPage", pagebean.getTotalPage());
        info.put("pageSize", pagebean.getPageSize());
        info.put("rows", pagebean.getAllRow());
        info.put("currentPage", pagebean.getCurrentPage());
        return Result.ok(info);

    }

}
